// 引入接口文件
import {
  deleteContent,down,copeContent,recall,submit
} from './commissionsDistRulesServ'
import { message } from 'antd'
import {cloneDeep} from 'lodash'
// import { hashHistory } from 'dva/router'
// import { makeAddress,download } from "utils/util"
import config from 'config/config'
import $ from 'jquery';

const defultState = {
    biz:{
      queryForm: {}
    },
    id:'',
    submitLoading: false,
    backLoading: false,
    rulesName: '',
}

const tmpModule = {
  state: cloneDeep(defultState),
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        if('' + tmpModule.pathname === '' + pathname){
          dispatch({ type: 'updateStore', payload: { ...cloneDeep(defultState), ...query } })//每次进去页面先重置状态
          
        }
      })
    },
  },


  effects: {
    *deleteContent({payload},{put,select,call}){
      let {id} = yield select(state => state[tmpModule.namespace])
      let params = {
        id: id,
      }
      const { success, msg, data } = yield call(deleteContent, {...params})
      let defultState = yield select(state => state[tmpModule.namespace])
      if(success){
        message.success('作废成功')
        defultState.biz.refreshListDatas(true)
      }
    },
    
    *down({ payload }, { put, call, select }) {
      let { biz,id,rulesName } = yield select(state => state[tmpModule.namespace])
      let params = { 
        id: id,
        name: rulesName,
        ...biz.queryForm 
      }
      let {data,success} = yield call(down, params)
      if (success) {
        var $a = $("<a>");
        $a.attr("href", data.href);
        $a.attr("download", '计佣规则明细');
        $("body").append($a);
        $a[0].click();
        $a.remove();
        message.success('导出成功,正在为您下载表格')
      }
    },

    *copeContent({payload},{put,select,call}){
      let {id,rulesName} = yield select(state => state[tmpModule.namespace])
      let params = {
        id: id,
        name: rulesName
      }
      const { success, msg, data } = yield call(copeContent, {...params})
      let defultState = yield select(state => state[tmpModule.namespace])
      if(success){
        message.success('复制方案成功')
        defultState.biz.refreshListDatas(true)
      }
    },
    *recall({payload},{put,select,call}){
      let {id} = yield select(state => state[tmpModule.namespace])
      yield put({type: 'updateStore', payload: {backLoading: true,}})
      let params = {
        id: id,
        tab: 1
      }
      const { success, msg, data } = yield call(recall, {...params})
      let defultState = yield select(state => state[tmpModule.namespace])
      if(msg){
        yield put({type: 'updateStore', payload: {backLoading: false,}})
      }
      if(success){
        message.success('撤回成功')
        defultState.biz.refreshListDatas(true)
      }
    },
    *check({payload},{put,select,call}){
      let {id} = yield select(state => state[tmpModule.namespace])
      // let url = `http://c3uat.midea.com/workflow/request/ViewRequest.jsp?requestid=${id}`
      let url = ''
      if(config.ENV === 'prod'){
        url = `http://c3.midea.com/workflow/request/ViewRequest.jsp?requestid=${id}`
      }else{
        url = `http://c3uat.midea.com/workflow/request/ViewRequest.jsp?requestid=${id}`
      }
      window.open(url)
    },
    *submit({payload},{put,select,call}){
      let {id} = yield select(state => state[tmpModule.namespace])
      yield put({type: 'updateStore', payload: {submitLoading: true,}})
      let params = {
        id: id,
        tab: 1
      }
      const { success, msg, data } = yield call(submit, {...params})
      let defultState = yield select(state => state[tmpModule.namespace])
      if(msg){
        yield put({type: 'updateStore', payload: {submitLoading: false,}})
      }
      if(success){
        message.success('提交成功')
        defultState.biz.refreshListDatas(true)
      }
    },
  },
  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
